import 'package:cps_app_network/cps_app_network.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<DataItem> result;

  @override
  void initState() {
    super.initState();
    request();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('测试'),
      ),
      body: ListView.builder(
        itemBuilder: (context, index) {
          final item = result[index];
          return ListTile(
            leading: Image.network('http:${item.pic}'),
            title: Text(item.title ?? ''),
            subtitle: Text(item.description ?? ''),
            trailing: Container(
              width: 100,
              child: Wrap(
                spacing: 3,
                runSpacing: 3,
                children: item.tag.split(',').map((e) {
                  return Container(
                    height: 16,
                    decoration: BoxDecoration(color: Colors.green),
                    child: Text(
                      e,
                      style: TextStyle(fontSize: 12, height: 1.3333),
                    ),
                  );
                }).toList(),
              ),
            ),
          );
        },
        itemCount: result == null ? 0 : result.length,
      ),
    );
  }

  void request() async {
    final data = await DggRequestApi(host: 'api.bilibili.com')
        .get('x/web-interface/search/type', query: {
      'keyword': '哲学',
      'search_type': 'video',
    });
    final model = Result.fromMap(data);
    result = model.data.result;
    setState(() {});
  }
}

class Result {
  final ListData data;

  Result({this.data});

  factory Result.fromMap(Map<String, dynamic> map) {
    return new Result(
      data: ListData.fromMap(map['data']),
    );
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'data': this.data,
    };
  }
}

class ListData {
  final List<DataItem> result;

  ListData({this.result});

  factory ListData.fromMap(Map<String, dynamic> map) {
    final list = map['result'];
    return ListData(
      result:
          list is List ? list.map((e) => DataItem.fromMap(e)).toList() : null,
    );
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'result': this.result,
    };
  }
}

class DataItem {
  final String title;
  final String description;
  final String pic;
  final String duration;
  final String tag;

  DataItem({this.title, this.description, this.pic, this.duration, this.tag});

  factory DataItem.fromMap(Map<String, dynamic> map) {
    return DataItem(
      title: map['title'],
      description: map['description'],
      pic: map['pic'],
      duration: map['duration'],
      tag: map['tag'],
    );
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'title': this.title,
      'description': this.description,
      'pic': this.pic,
      'duration': this.duration,
      'tag': this.tag,
    };
  }
}
